﻿using Server.Framework.Service;
using Server.OptExp;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Server.Util
{
    class CommonTool
    {
        /// <summary>
        /// 根据页码和页面条数转换查询起始和结束值
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">条数</param>
        /// <returns>结果：数组第一位为start  第二位为end</returns>
        public static int[] ConvertStartEnd(int page, int limit)
        {
            int[] res = new int[2];
            int start = (page - 1) * limit;
            int end = page * limit;
            res[0] = start;
            res[1] = end;
            return res;
        }

        /// <summary>
        /// 获取数据行数
        /// </summary>
        /// <param name="TableName">数据表名</param>
        /// <returns>数据行数</returns>
        public static int GetRowCount(string TableName, string conStr = "")
        {
            int count = 0;
            string queryCountSql = $"SELECT COUNT(ID) FROM {TableName} WHERE DEL_FLAG = 1 {conStr}";
            DbConnection connection = DBService.Instance().GetConnection();
            try
            {
                connection.Open();
                using (DbCommand command = connection.CreateCommand())
                {
                    command.CommandText = queryCountSql;
                    using (DbDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["COUNT(ID)"]);
                        }
                    }
                }
                connection.Close();
                return count;
            }
            catch (Exception e)
            {
                throw new CommonOptException(e.Message);
            }finally
            {
                connection.Close();
            }
        }




    }
}
